package co.yixiang.modules.shop.service.impl;

import co.yixiang.modules.shop.entity.YxInvestCustomer;
import co.yixiang.modules.shop.entity.YxInvestOrder;
import co.yixiang.modules.shop.mapper.YxInvestCustomerMapper;
import co.yixiang.modules.shop.mapper.YxInvestOrderMapper;
import co.yixiang.modules.shop.mapping.InvestCustomerMap;
import co.yixiang.modules.shop.service.YxInvestCustomerService;
import co.yixiang.modules.shop.web.param.YxInvestCustomerQueryParam;
import co.yixiang.modules.shop.web.vo.YxInvestCustomerQueryVo;
import co.yixiang.common.service.impl.BaseServiceImpl;
import co.yixiang.common.web.vo.Paging;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 * 投资客户表 服务实现类
 * </p>
 *
 * @author hisen
 * @since 2020-06-15
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class YxInvestCustomerServiceImpl extends BaseServiceImpl<YxInvestCustomerMapper, YxInvestCustomer> implements YxInvestCustomerService {

    @Autowired
    private YxInvestCustomerMapper yxInvestCustomerMapper;
    @Autowired
    private InvestCustomerMap investCustomerMap;
    @Autowired
    private YxInvestOrderMapper yxInvestOrderMapper;


    @Override
    public List<YxInvestCustomerQueryVo> customerList(int uid, int page, int limit, List<Integer> saleId){
        QueryWrapper<YxInvestCustomer> wrapper= new QueryWrapper<>();
        if(saleId != null && uid >0){
            wrapper.in("sales_man_id",saleId);
        }
        else if(uid > 0 &&  saleId == null) wrapper.eq("sales_man_id",uid);

        wrapper.orderByDesc("add_time");
        Page<YxInvestCustomer> pageModel = new Page<>(page, limit);

        IPage<YxInvestCustomer> pageList = yxInvestCustomerMapper.selectPage(pageModel,wrapper);
        List<YxInvestCustomerQueryVo> list = investCustomerMap.toDto(pageList.getRecords());
        List<YxInvestCustomerQueryVo> newList = new ArrayList<YxInvestCustomerQueryVo>();
        for(YxInvestCustomerQueryVo customer : list){
            String investPhone = customer.getInvestPhone();
            QueryWrapper<YxInvestOrder> orderWrapper= new QueryWrapper<>();
            orderWrapper.eq("invest_phone",investPhone).ne("invest_status",0).ne("invest_status",1).ne("invest_status",2).ne("invest_status",9).orderByAsc("add_time");
             //认购订单数
            customer.setInvestedCount(yxInvestOrderMapper.selectCount(orderWrapper));
            List<YxInvestOrder> orderList = yxInvestOrderMapper.selectList(orderWrapper);
            if(orderList.size() >0 ){
                //认购时间(取最早时间)
                customer.setInvestDate(orderList.get(0).getAddTime());
                //认购结束时间(取最早时间)
                customer.setEndDate(orderList.get(0).getEndDate());
            }
            newList.add(customer);
        }

        return newList;
    }
    @Override
    public YxInvestCustomer getByPhone(String phone){
        QueryWrapper<YxInvestCustomer> wrapper= new QueryWrapper<>();
        wrapper.eq("invest_phone",phone);
        return yxInvestCustomerMapper.selectOne(wrapper);
    }



}
